package com.dashu.core.controller;

import com.dashu.base.enums.ReplRyCode;
import com.dashu.core.dto.PageDTO;
import com.dashu.core.result.PageEnty;
import com.dashu.core.result.ResultBean;
import com.dashu.core.dto.company.CompanyDTO;
import com.dashu.core.service.CompanyService;
import com.dashu.core.vo.company.CompanyListVO;
import com.dashu.core.vo.company.CompanyVO;
import com.dashu.core.vo.UpdateEntityVO;
import com.dashu.core.vo.company.UpCompanyVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * @Author xys
 * @Date 2020/ReplRyCode.SUCCESS.getCode()ReplRyCode.SUCCESS.getCode()/ReplRyCode.SUCCESS.getCode()7 ReplRyCode.SUCCESS.getCode()5:2ReplRyCode.SUCCESS.getCode()
 */

@RequestMapping("dashu/company")
@RestController
@Slf4j
@Api(value = "dashu/company", tags = {"单位模块"}, description = "后台")
public class CompanyController {

    @Autowired
    private CompanyService companyService;
//    @Autowired
//    RestTemplate restTemplate;

    @PostMapping("/addCompany")
    @ApiOperation(value = "单位添加", notes = "单位添加")
    public ResultBean<CompanyDTO> addCompany(@RequestBody @Validated CompanyVO companyVO) {
        CompanyDTO companyDTO = companyService.existByCompanyName(companyVO.getCompanyName(),0);
         companyService.isAdd(companyVO);
        if (companyDTO != null)
            return new ResultBean<>(ReplRyCode.EXIST_COMPANY.getCode(), ReplRyCode.EXIST_COMPANY.getMessage(), null);

        if (companyVO == null) {
            return new ResultBean<>(ReplRyCode.DATA_NOT_NULL.getCode(), ReplRyCode.DATA_NOT_NULL.getMessage(), null);
        }
        return new ResultBean<>(ReplRyCode.SUCCESS.getCode(), "单位添加", companyService.add(companyVO));
    }

    @PostMapping("/updataCompany")
    @ApiOperation(value = "单位信息修改", notes = "单位信息修改")
    public ResultBean<CompanyDTO> updataPass(@RequestBody @Validated UpdateEntityVO<UpCompanyVO> updateCompanyVO) throws Exception {
        if (updateCompanyVO == null) {
            return new ResultBean<>(ReplRyCode.DATA_NOT_NULL.getCode(), ReplRyCode.DATA_NOT_NULL.getMessage(), null);
        }
        return new ResultBean<>(ReplRyCode.SUCCESS.getCode(), "单位信息修改", companyService.updataByCompanyId(updateCompanyVO));

    }

    @PostMapping("/findBase")
    @ApiOperation(value = "父节点查询部门列表", notes = "父节点查询部门列表")
    public ResultBean<List<CompanyDTO>> findBase(@RequestBody @Validated PageEnty<Long> creteUserVo) {
        if (creteUserVo == null) {
            return new ResultBean<>(ReplRyCode.DATA_NOT_NULL.getCode(), ReplRyCode.DATA_NOT_NULL.getMessage(), null);
        }
        return new ResultBean<>(ReplRyCode.SUCCESS.getCode(), "父节点查询部门列表", companyService.findByCreteUser(creteUserVo));
    }

    @PostMapping("/findCompany")
    @ApiOperation(value = "单位信息查询", notes = "单位信息查询")
    public ResultBean<CompanyDTO> findCompany(long companyId) {
        if (companyId == 0) {
            return new ResultBean<>(ReplRyCode.DATA_NOT_NULL.getCode(), ReplRyCode.DATA_NOT_NULL.getMessage(), null);
        }
        return new ResultBean<>(ReplRyCode.SUCCESS.getCode(), "子用户查询", companyService.findByCompanyId(companyId));
    }

    @PostMapping("/findCompanyName")
    @ApiOperation(value = "单位名称信息查询(模糊搜索)", notes = "单位名称信息查询")
    public ResultBean<List<CompanyDTO>> findCompanyName(String companyName, long baseId) {
        if (companyName == null) {
            return new ResultBean<>(ReplRyCode.DATA_NOT_NULL.getCode(), ReplRyCode.DATA_NOT_NULL.getMessage(), null);
        }
        return new ResultBean<>(ReplRyCode.SUCCESS.getCode(), "单位名称信息查询", companyService.findByCompanyName(companyName, baseId));
    }

    @DeleteMapping("/deleteCompany")
    @ApiOperation(value = "删除单位信息", notes = "删除单位信息")
    public ResultBean<Boolean> deleteCompany(long companyDepartmentId) throws Exception {
        if (companyDepartmentId == 0) {
            return new ResultBean<>(ReplRyCode.DATA_NOT_NULL.getCode(), ReplRyCode.DATA_NOT_NULL.getMessage(), null);
        }
        return new ResultBean<>(ReplRyCode.SUCCESS.getCode(), "删除单位信息", companyService.deleteByCompanyId(companyDepartmentId));
    }

    @PostMapping("/pageCompanyList")
    @ApiOperation(value = "单位信息列表", notes = "单位信息列表")
    public ResultBean<PageDTO<CompanyDTO>> pageCompanyList(@RequestBody @Validated PageEnty<CompanyListVO> companyListPageEntyVO) throws Exception {
        PageDTO<CompanyDTO> pageDTO = new PageDTO<>();
        if (companyListPageEntyVO == null) {
            return new ResultBean<>(ReplRyCode.DATA_NOT_NULL.getCode(), ReplRyCode.DATA_NOT_NULL.getMessage(), null);
        }
        long total = companyService.companyCount(companyListPageEntyVO);
        pageDTO.setTotal(total);
        pageDTO.setDataList(companyService.pageCompanyList(companyListPageEntyVO));
        return new ResultBean<>(ReplRyCode.SUCCESS.getCode(), "单位信息列表", pageDTO);
    }

}
